package edu.ucsur.tp.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import com.sun.xml.internal.txw2.Document;

import edu.ucsur.tp.beans.ClienteBean;
import edu.ucsur.tp.beans.UsuarioBean;
import edu.ucsur.tp.data.MyData;
import edu.ucsur.tp.data.ut;

public class ClienteDAOMyBatis implements ClienteDAO {
	
	 private SqlSessionFactory sqlSessionFactory;
	 private SqlSession session = null;
	  
	
	 public ClienteDAOMyBatis(SqlSessionFactory sqlSessionFactory) {
		    this.sqlSessionFactory = sqlSessionFactory;
	 }
	
	
	@Override
	public List<ClienteBean> obtenerTodos() {
		try {
		      session = sqlSessionFactory.openSession();
		      return session.selectList("edu.ucsur.tp.dao.ClienteMapper.obtenerTodos");
		} catch (Exception ex) {
		      ex.printStackTrace();
		} finally {
		      session.close();
		}
		return null;
	}

	@Override
	public ClienteBean obtener(String codigo) {
		try {
		      session = sqlSessionFactory.openSession();
		      return session.selectOne("edu.ucsur.tp.dao.ClienteMapper.obtener", codigo);
	    } catch (Exception ex) {
	    	  ex.printStackTrace();
	    } finally {
	          session.close();
	    }
	    return null;
			
	}

	@Override
	public void insertar(ClienteBean c) {
		MyData oData = new MyData();
		
		String xSql;
		
		oData.AddValues(c.getApellidos());
		oData.AddValues(c.getNombres());
		oData.AddValues(c.getCorreo());
		oData.AddValues(c.getDireccion());
		oData.AddValues(c.getEstado());
		oData.AddValues(c.getFechanac());
		oData.AddValues(c.getGenero());
		oData.AddValues(c.getId_pais());
		oData.AddValues(c.getId_ubigeo());
		oData.AddValues(c.getNrodoc());
		oData.AddValues(c.getTelefono1());
		oData.AddValues(c.getTelefono2());
		oData.AddValues(c.getTelefono3());
		
		xSql = "Insert Into clientes (apellidos, nombres, correo, direccion, estado, "
									+ "fechanac, genero, id_pais, id_ubigeo, nrodoc,"
									+ "telefono1, telefono2, telefono3) values ("
									+ "?,?,?,?,?,?,?,?,?,?,?,?,?)";
		oData.Execute(xSql,true);
		oData.Dispose();
	}

	@Override
	public void actualizar(ClienteBean c) {
		MyData oData = new MyData();
		
		String xSql;
		
		oData.AddValues(c.getApellidos());
		oData.AddValues(c.getNombres());
		oData.AddValues(c.getCorreo());
		oData.AddValues(c.getDireccion());
		oData.AddValues(c.getEstado());
		oData.AddValues(c.getFechanac());
		oData.AddValues(c.getGenero());
		oData.AddValues(c.getId_pais());
		oData.AddValues(c.getId_ubigeo());
		oData.AddValues(c.getNrodoc());
		oData.AddValues(c.getTelefono1());
		oData.AddValues(c.getTelefono2());
		oData.AddValues(c.getTelefono3());
		
		xSql = "Update clientes set apellidos = ?, nombres = ?, correo = ?, direccion = ?, estado = ?, "
									+ "fechanac = ?, genero = ?, id_pais = ?, id_ubigeo = ?, nrodoc = ?,"
									+ "telefono1 = ?, telefono2 = ?, telefono3 = ? "
									+ " where id_cliente=" + c.getId_cliente();
		oData.Execute(xSql,true);
		oData.Dispose();
		
	}

	@Override
	public void eliminar(String codigo) {
		MyData oData = new MyData();
		String xSql = "delete from clientes where id_cliente=" + codigo;
		oData.Execute(xSql, false);
		oData.Dispose();
	}

	@Override
	public List<ClienteBean> obtenerFiltro(String Apellido, String Documento) {
		
		List<ClienteBean> Lista = new ArrayList<ClienteBean>();
		boolean filtro = false;
		
		MyData oData = new MyData();
		String xSql;
		
		xSql = "SELECT * from clientes ";
		
		//Filtros
		if (Apellido.trim().length()>0){
			xSql += " where apellidos like " +  ut.StrAp(Apellido + "%");
			filtro = true ;
		}
		
		if (Documento.trim().length()>0){
			if (filtro) {
				xSql += " and nrodoc like " +  ut.StrAp(Documento + "%");
			} else {
				xSql += " where nrodoc like " +  ut.StrAp(Documento + "%");
			}
		}
		
		ResultSet rs = oData.GetCursor(xSql);
		try {
			while (rs.next()){
				ClienteBean cli = new ClienteBean();
				cli.setId_cliente(rs.getInt("Id_cliente"));
				cli.setNombres(rs.getString("Nombres"));
				cli.setApellidos(rs.getString("Apellidos"));
				cli.setNrodoc(rs.getString("nrodoc"));
				cli.setCorreo(rs.getString("correo"));
				cli.setTelefono1(rs.getString("telefono1"));
				cli.setFechanac(rs.getDate("fechanac"));
				Lista.add(cli);
			} 
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return Lista;
	}

}
